class DataSearch {
    indexArray = [];
    push(value, indexField = false) {
        let keyword = '';
        if (!indexField) {
            if (value.name) keyword += value.name;
            if (value.title) keyword += value.title;
        } else if (typeof indexField === 'function') {
            keyword = indexField(value);
        } else if (typeof indexField === 'string') {
            keyword = value[indexField];
        } else {
            keyword = JSON.stringify(value);
        }
        this.indexArray.push({ keyword, value });
    }
    pushAll(values, indexField = false) {
        values.map(v => this.push(v));
    }
    search(keyword) {
        let keywordArray = keyword.split(' ').filter(v => !!v);
        let result = [];
        keywordArray.map(keyword => result = result.concat(this.indexArray.filter(v => v.keyword.indexOf(keyword) > -1)));
        return [...new Set(result)];
    }
}

/**
const searcher = new DataSearch();
searcher.pushAll([{name: 'dasd'},{title: 'gggg'}]);
// 简单搜索
searcher.search('d');
// 多关键词联合
searcher.search('d g');
 */